package cn.wolfcode.wolf2w.search.service.impl;

import cn.wolfcode.wolf2w.search.api.domain.UserInfoEs;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.common.core.utils.bean.BeanUtils;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.repository.UserInfoEsRepository;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserInfoEsServiceImpl implements IUserInfoEsService {

    @Autowired
    private UserInfoEsRepository repository;
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Override
    public void save(UserInfoEs userInfo) {
        //userInfo.setId(null);
        repository.save(userInfo);
    }

    @Override
    public void update(UserInfoEs userInfo) {
        repository.save(userInfo);
    }

    @Override
    public UserInfoEs get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfoEs> list() {
        List<UserInfoEs> list = new ArrayList<>();
        Iterable<UserInfoEs> all = repository.findAll();
        all.forEach(a -> list.add(a));
        return list;
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }


    @Override
    public Page<UserInfo> queryByKeyWord(SearchQueryObject qo) {
        String keyword = qo.getKeyword();
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());

        SearchPage<UserInfoEs> searchPage = repository.queryByCityOrNicknameOrInfo(keyword, keyword, keyword, pageRequest);

        List<UserInfo> list = searchPage.getSearchHits().stream().map(searchHit -> {
            R<UserInfo> ret = remoteUserInfoService.getOne(Long.parseLong(searchHit.getId()), SecurityConstants.INNER);
            UserInfo data = ret.getData();
            StringUtils.handleESHighlight(searchHit.getHighlightFields(), data);
            return data;
        }).collect(Collectors.toList());

        return new PageImpl<UserInfo>(list, pageRequest, searchPage.getTotalElements());
    }

    @Override
    public void initUserinfoEs() {
        R<List<UserInfo>> result = remoteUserInfoService.list(SecurityConstants.INNER);
        List<UserInfo> list = result.getData();

        List<UserInfoEs> esList = list.stream().map(record -> {
            UserInfoEs userInfoEs = new UserInfoEs();
            BeanUtils.copyProperties(record, userInfoEs);
            return userInfoEs;
        }).collect(Collectors.toList());

        repository.saveAll(esList);
    }

}
